package com.hb.trans.mvp.ui.fragment


import android.annotation.SuppressLint
import android.view.View
import android.widget.GridView
import android.widget.ImageView
import cn.bingoogolapple.bgabanner.BGABanner
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseFragment
import com.bumptech.glide.Glide
import com.hb.trans.R
import com.hb.trans.bean.Article
import com.hb.trans.bean.Banner
import com.hb.trans.bean.HomeList
import com.hb.trans.bean.Navigation
import com.hb.trans.global.*
import com.hb.trans.mvp.contract.HomeContract
import com.hb.trans.mvp.presenter.HomePresenter
import com.hb.trans.mvp.ui.adapter.HomeAdapter
import com.hb.trans.mvp.ui.adapter.NavigationAdapter
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_home.*
import javax.inject.Inject

class HomeFragment : BaseFragment<HomePresenter>(), HomeContract.View, BGABanner.Adapter<ImageView, Banner>,
    BGABanner.Delegate<ImageView, Banner> {
    override fun setViewId(): Int {
        return R.layout.fragment_home
    }

    override fun doOnInvisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doOnVisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Inject
    lateinit var mPresenter: HomePresenter
    @Inject
    lateinit var mAdapter: HomeAdapter
    lateinit var headerView: View
    lateinit var homeGv: GridView
    lateinit var mBanner: BGABanner
//    lateinit var rootView: View

    companion object {
        fun newInstance(): HomeFragment {
            val fragment = HomeFragment()
            return fragment
        }
    }

//    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
//        rootView = inflater.inflate(R.layout.fragment_home, container, false)
//        return rootView
//    }

    override fun onFragmentInject() {
        AndroidSupportInjection.inject(this)
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun initView() {
        initHeader()
        mAdapter.addHeaderView(headerView)
//        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT)
        rvHome.apply {
            layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    @SuppressLint("CheckResult")
    private fun initHeader() {
        headerView = View.inflate(context, R.layout.layout_home_header, null)
        mBanner = headerView.findViewById(R.id.banner)
        homeGv = headerView.findViewById(R.id.homegv)
        mBanner.setAdapter(this)
        mBanner.setDelegate(this)
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, _, position ->
            val new = adapter.getItem(position) as Article
            ARouter.getInstance().build(ACTIVITY_NEW_DETAIL)
                .withString("articleId", new.id)
                .navigation()
        }
        homeGv.setOnItemClickListener { _, _, position, _ ->
            val navigation = homeGv.adapter.getItem(position) as Navigation
            navigation?.let {
                when (it.id) {
                    "0c0048566d504336b5f2f29e112f9932" -> {
                        ARouter.getInstance().build(ACTIVITY_TRANS_CHECK_LOGIN).navigation()
                    }
                    "4727b28599c54573a42081c812cedb9f" -> {
                        ARouter.getInstance().build(ACTIVITY_HOME_REGISTER).navigation()
                    }
                    else -> {
                        if (it.title.equals("在线学习")) {
//                            showShortToast("暂未开通", SnackbarUtil.INFO)
                            ARouter.getInstance().build(ACTIVITY_FLUTTER)
                                .withString("pageIndex", "company")
                                .navigation()
                            return@let
                        }
                        ARouter.getInstance().build(ACTIVITY_WEB).withString("url", it.redirect_url).navigation()
                    }
                }
            }
        }
    }

    override fun initData() {

    }

    override fun setUpdata(data: HomeList?) {
        data?.let {
            mBanner.setData(data.banners, null)
            val navigation = data.navigations.filter { it.home_entrance == 1 && it.status == 1 }.toMutableList()
            homeGv.numColumns = if (navigation.size < 4) navigation.size else 4;
            homeGv.adapter = NavigationAdapter(mContext, navigation, R.layout.item_serve)
            mAdapter.setNewData(data.articles)
        }
    }

    override fun onBannerItemClick(banner: BGABanner?, itemView: ImageView?, model: Banner?, position: Int) {
        ARouter.getInstance()
            .build(ACTIVITY_WEB)
            .withString("url", model?.redirect_url)
            .navigation()
    }

    override fun fillBannerItem(banner: BGABanner?, itemView: ImageView?, model: Banner?, position: Int) {
        itemView?.let {
            Glide.with(this)
                .load(model?.pic)
                .into(itemView)
        }
    }

}
